package mycustom.webservice;

import com.alibaba.fastjson.JSON;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.common.util.string.StringUtil;
import weaver.conn.RecordSet;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DataChangeServiceImpl implements DataChangeService {

    Log log = LogFactory.getLog(DataChangeServiceImpl.class);


    public String getUserByWorkCode(String WorkCode) {
        User user = new User();
        String returnJson = "";
        String sql = "select id,departmentid,subcompanyid1 from hrmresource where workcode = '" + WorkCode + "'";
        log.info("getUserByWorkCode---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            user.setUserId(rs.getInt("id"));
            user.setDepartmentId(rs.getInt("departmentid"));
            user.setSubCompanyId(rs.getInt("subcompanyid1"));
        }
        try {
            returnJson = JSON.toJSONString(user);
            log.info("getUserByWorkCode---return:" + returnJson);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnJson;
    }

    public Integer getDepartmentIdByName(String DepartmentName) {
        Integer DepartmentId = -1;
        String sql = "select id from hrmdepartment where departmentmark='" + DepartmentName + "' or departmentname='" + DepartmentName + "'";
        log.info("getDepartmentIdByName---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            DepartmentId = rs.getInt("id");
        }
        log.info("getDepartmentIdByName---DepartmentId:" + DepartmentId);
        return DepartmentId;
    }

    public Integer getSubcompanyIdByName(String SubComanyName) {
        Integer SubcompanyId = -1;
        String sql = "select id from hrmsubcompany where subcompanyname ='" + SubComanyName + "' or subcompanydesc='" + SubComanyName + "'";
        log.info("getSubcompanyIdByName---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            SubcompanyId = rs.getInt("id");
        }
        log.info("getSubcompanyIdByName---SubcompanyId:" + SubcompanyId);
        return SubcompanyId;
    }

    public Integer getBrowseIdByName(String TableName, String Condition, String Value) {
        Integer BrowseId = -1;
        String sql = "select id from " + TableName + " where " + Condition + " = '" + Value + "'";
        log.info("getBrowseIdByName---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            BrowseId = rs.getInt("id");
        }
        log.info("getBrowseIdByName---BrowseId:" + BrowseId);
        return BrowseId;
    }

    public Integer getDroplistByName(String WorkFlowName, String Condition, String Value) {
        Integer selectValue = -1;
        String sql = "select distinct(t1.fieldid),(select fieldname from workflow_billfield where id = t1.fieldid) ms,\n" +
                "t1.id,t1.selectvalue,t1.selectname,t4.workflowname\n" +
                "from workflow_selectitem t1,workflow_nodeform t2,workflow_flownode t3,workflow_base t4\n" +
                "where t1.fieldid = t2.fieldid and t2.nodeid = t3.nodeid and t3.workflowid = t4.id\n" +
                "and t4.workflowname = '" + WorkFlowName + "'\n" +
                "and (select fieldname from workflow_billfield where id = t1.fieldid) = '" + Condition + "'\n" +
                "and t1.selectname='" + Value + "'\n" +
                "order by t1.fieldid ";

        log.info("getDroplistByName---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            selectValue = rs.getInt("selectvalue");
        }
        log.info("getDroplistByName---selectValue:" + selectValue);
        return selectValue;
    }

    public String getProjectInfoById(Integer ProjectId) {
        ProjectInfo projectInfo = new ProjectInfo();
        String returnJson = "";
        String sql = "select xmh,xmlh,xmjl,xmzs from uf_xmhdb where id=" + ProjectId;
        log.info("getProjectInfoById---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            projectInfo.setXmh(rs.getString("xmh"));
            projectInfo.setXmlh(rs.getString("xmlh"));
            projectInfo.setXmjl(rs.getInt("xmjl"));
            projectInfo.setXmzs(rs.getString("xmzs"));
        }
        try {
            returnJson = JSON.toJSONString(projectInfo);
            log.info("getProjectInfoById---return:" + returnJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnJson;
    }

    public Integer getUnitIdByName(String UnitName) {
        Integer UnitId = -1;
        String sql = "select id from LgcAssetUnit where unitname='" + UnitName + "' or unitdesc='" + UnitName + "'";
        log.info("getUnitIdByName---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            UnitId = rs.getInt("id");
        }
        log.info("getUnitIdByName---UnitId:" + UnitId);
        return UnitId;
    }

    public Integer getCurrencyIdByName(String CurrencyName) {
        Integer CurrencyId = -1;
        String sql = "select id from fnacurrency where currencyname='" + CurrencyName + "' or currencydesc='" + CurrencyName + "'";
        log.info("getCurrencyIdByName---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            CurrencyId = rs.getInt("id");
        }
        log.info("getCurrencyIdByName---CurrencyId:" + CurrencyId);
        return CurrencyId;
    }

    public String getActiveVersionID(String workflowid) {
        String activeVersionID = "";
        String sql = "select activeVersionID from workflow_base where id = " + workflowid;
        log.info("getActiveVersionID---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            activeVersionID = rs.getString("activeVersionID");
        }
        if (StringUtil.isNullOrEmpty(activeVersionID) || activeVersionID == "null") {
            activeVersionID = workflowid;
        }
        log.info("getActiveVersionID---activeVersionID:" + activeVersionID);
        return activeVersionID;
    }

    public String getMaxHetlsh(String hetlx) {
        if (hetlx.equals("产品销售")) {
            hetlx = "YZXS-";
        } else if (hetlx.equals("服务销售")) {
            hetlx = "YZFW-";
        } else if (hetlx.equals("租赁")) {
            hetlx = "YZZL-";
        } else {
            hetlx = "";
        }

        int maxNumber = 0;//申请日期最大流水号，四位
        //获取当前日期，格式：yyyymmdd
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyyMMdd");
        String currentDate = format.format(date);
        //获取数据表返回的最大流水号
        String sql = "select isNULL(max(cast(hetlsh3 as int)),0) maxNumber from ( " +
                " select SUBSTRING(hetlsh,6,8) hetlsh1,RIGHT(hetlsh,4) hetlsh2,replace(ltrim(replace(RIGHT(hetlsh,4) ,'0',' ')),' ','0') hetlsh3 from uf_httz where hetlsh like '%" + currentDate + "%' ) t";
        log.info("getMaxHetlsh---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            maxNumber = rs.getInt("maxNumber");
        }
        //自增流水号
        maxNumber = (maxNumber == -1 ? 1 : (maxNumber + 1));
        log.info("getMaxHetlsh---maxNumber:" + maxNumber);
        //拼接合同流水号
        String hetlsh = hetlx + currentDate + "-" + String.format("%04d", maxNumber);
        return hetlsh;
    }

    public Integer getModeDroplistByName(String fieldid, String Value) {
        Integer selectValue = -1;
        String sql = "select t1.*,t2.fieldname from workflow_selectitem t1,workflow_billfield t2 " +
                " where t1.fieldid = t2.id and t1.selectname = '" + Value + "' and t1.fieldid = '" + fieldid + "'";
        log.info("getModeDroplistByName---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            selectValue = rs.getInt("selectvalue");
        }
        log.info("getModeDroplistByName---selectValue:" + selectValue);
        return selectValue;
    }

    //通过表名:TableName，条件字段名:Condition，传入参数:Param，查找对应的值:Value
    public String getVauleFromTableByParam(String TableName, String Condition, String SelectValue, String Param) {
        String Value = "";
        String sql = "select " + SelectValue + " from " + TableName + " where " + Condition + " = '" + Param + "'";
        log.info("getVauleFromTableByParam---sql:" + sql);
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if (rs.next()) {
            Value = rs.getString(SelectValue);
        }
        log.info("getVauleFromTableByParam---Value:" + Value);
        return Value;
    }

}
